package com.kucowka.store.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kucowka.store.dto.Product;

@Controller(value = "testController")
@RequestMapping(value = "/actions")
public class TestController {

	private static final Logger logger = Logger.getLogger(TestController.class);

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public void testGet(HttpServletResponse response, HttpServletRequest request) {
		logger.info("HTTP GET TEST LOG");
		System.out.println("HTTP GET TEST LOG");

		response.setStatus(HttpServletResponse.SC_OK);
	}

	@RequestMapping(value = "/selectedPeople", method = RequestMethod.POST)
	public void registerPeople(
			@RequestParam(value = "names[]", required = true) String[] names,
			HttpServletResponse response) {
		logger.info("HTTP POST registerPeople, got: " + names.length);
		System.out.println("HTTP POST registerPeople, got: " + names.length);

		for (String name : names) {
			System.out.println("People: " + name);
		}

		response.setStatus(HttpServletResponse.SC_OK);
	}

	@RequestMapping(value = "/product", method = RequestMethod.GET, headers = "Accept=*/*")
	public @ResponseBody
	JsonResponse testGet(@RequestParam(required = false) String name) {
		logger.info("HTTP GET Product info LOG");
		System.out.println("HTTP GET Product info LOG " + name);

		Product product = new Product();
		product.setName("book");
		product.setValue("56.90$");

		JsonResponse response = new JsonResponse();
		response.setResult(product);
		response.setStatus("SUCCESS");

		return response;
	}

}
